package cn.buildoncloud.labor.workermanapplet.util;

import cn.buildoncloud.labor.workermanapplet.common.base.Result;
import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * OCR和虹软相关的工具类
 * Description: cn.buildoncloud.labor.workermanapplet.util
 * Created by CuiZibo on 2018/11/13 18:01
 */
@Component
public class OCRUtil {

    private static String staticWebSystemIP;

    @Value("${webSystemIP}")
    private String webSystemIP;

    @PostConstruct
    public void construct() {
        staticWebSystemIP = this.webSystemIP;
    }

    public static String getStaticWebSystemIP() {
        return staticWebSystemIP;
    }

    /**
     * OCR识别身份证正面
     * @param base64File
     * @return
     */
    public static Result ocrIdCard(String base64File) {
        MultipartFile file = FileUtil.base64ToMultipart(base64File);
        String response = HttpUtil.postFile(staticWebSystemIP + "/fileupload/ocrIdCard", Arrays.asList(file), "file", null, 60000);
        Result result = Json.fromJson(Result.class, response);
        return result;
    }

    /**
     * 优化身份证照片
     * @param photo
     * @return
     */
    public static Result optimizeIdCard(String photo) {
        Map map = new HashMap();
        map.put("photo",photo);
        String response = HttpUtil.post(staticWebSystemIP + "/face/decorativeIdCardPhoto", map, 60000);
        Result result = Json.fromJson(Result.class, response);
        return result;
    }

    /**
     * OCR识别营业执照
     * @param base64File
     * @return
     */
    public static Result ocrBizLicense(String base64File) {
        MultipartFile file = FileUtil.base64ToMultipart(base64File);
        String response = HttpUtil.postFile(staticWebSystemIP + "/fileupload/ocrUpload", Arrays.asList(file), "file", null, 60000);
        Result result = Json.fromJson(Result.class, response);
        return result;
    }

    /**
     * http传输文件例子
     */
    public static Result ocrIdCard2(List<MultipartFile> files) {

        String response = HttpUtil.postFile(staticWebSystemIP + "/fileupload/ocrIdCard", files, "file", new HashMap<>(), 10000);
        Result result = Json.fromJson(Result.class, response);
        return result;
    }
}
